--- title: "Example Presentation" output: pdf_document date: "2024-09-09" ---
h <- h %>%
filter(raceYear=="2020")
president_data <- president_2020 %>%
filter(X2020!="Biden") %>%
rename(CD=X)
president_data <- left_join (president_data,h,by="CD")
president_data <- president_data %>%
select(X2020,X.3,State,district_num,raceYear)
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
cd117_pa <- cd117 %>%
filter(STATENAME=="Pennsylvania") %>%
mutate(DISTRICT = as.character(DISTRICT))%>%
select(DISTRICT)
# add data to plot - 2014 GOP party seat share
# reload election data - h from previous exercise
h <- president_data
# filter for 2014 election and state
R_pa_2020 <- h %>%
filter(raceYear == 2020, State == "Pennsylvania") %>%
select(raceYear, State, district_num, X.3, X2020) %>%
# summarize party vote share by district
group_by(district_num) %>%
summarise(Trump_margin = as.numeric(X.3)-as.numeric(X2020)) %>%
# rename district variable name to match shapefile
rename(DISTRICT = district_num)
# change class
cd117_pa$DISTRICT <- as.numeric(cd117_pa$DISTRICT)
# join election returns with shapefiles
cd117_pa <- cd117_pa %>% left_join(R_pa_2020, by="DISTRICT")
suppressPlotlyMessage <- function(p) {
suppressMessages(plotly_build(p))
}
# time to map!
pamap <- ggplot() +
geom_sf(data=cd117_pa,aes(fill=Trump_margin),
inherit.aes=FALSE,alpha=0.9) +
scale_fill_gradient2(high="red",mid="white",low="blue") +
theme_void() + ggtitle ("Donald Trump Vote Share in 2020 PA Presidential Elections")
fig2 <- suppressPlotlyMessage(ggplotly(pamap))
fig2
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.
cd117_wi <- cd117 %>%
filter(STATENAME=="Wisconsin") %>%
mutate(DISTRICT = as.character(DISTRICT))%>%
select(DISTRICT)
# add data to plot - 2014 GOP party seat share
# reload election data - h from previous exercise
h <- president_data
# filter for 2014 election and state
R_wi_2020 <- h %>%
filter(raceYear == 2020, State == "Wisconsin") %>%
select(raceYear, State, district_num, X.3, X2020) %>%
# summarize party vote share by district
group_by(district_num) %>%
summarise(Trump_margin = as.numeric(X.3)-as.numeric(X2020)) %>%
# rename district variable name to match shapefile
rename(DISTRICT = district_num)
# change class
cd117_wi$DISTRICT <- as.numeric(cd117_wi$DISTRICT)
# join election returns with shapefiles
cd117_wi <- cd117_wi %>% left_join(R_wi_2020, by="DISTRICT")
suppressPlotlyMessage <- function(p) {
suppressMessages(plotly_build(p))
}
# time to map!
wimap <- ggplot() +
geom_sf(data=cd117_wi,aes(fill=Trump_margin),
inherit.aes=FALSE,alpha=0.9) +
scale_fill_gradient2(high="red",mid="white",low="blue") +
theme_void() + ggtitle ("Donald Trump Vote Share in 2020 WI Presidential Election")
fig3 <- suppressPlotlyMessage(ggplotly(wimap))
fig3
cd117_ga <- cd117 %>%
filter(STATENAME=="Georgia") %>%
mutate(DISTRICT = as.character(DISTRICT))%>%
select(DISTRICT)
# add data to plot - 2014 GOP party seat share
# reload election data - h from previous exercise
h <- president_data
# filter for 2014 election and state
R_ga_2020 <- h %>%
filter(raceYear == 2020, State == "Georgia") %>%
select(raceYear, State, district_num, X.3, X2020) %>%
# summarize party vote share by district
group_by(district_num) %>%
summarise(Trump_margin = as.numeric(X.3)-as.numeric(X2020)) %>%
# rename district variable name to match shapefile
rename(DISTRICT = district_num)
# change class
cd117_ga$DISTRICT <- as.numeric(cd117_ga$DISTRICT)
# join election returns with shapefiles
cd117_ga <- cd117_ga %>% left_join(R_ga_2020, by="DISTRICT")
suppressPlotlyMessage <- function(p) {
suppressMessages(plotly_build(p))
}
# time to map!
gamap <- ggplot() +
geom_sf(data=cd117_ga,aes(fill=Trump_margin),
inherit.aes=FALSE,alpha=0.9) +
scale_fill_gradient2(high="red",mid="white",low="blue") +
theme_void() + ggtitle ("Donald Trump Vote Share in 2020 GA Presidential Election")
fig4 <- suppressPlotlyMessage(ggplotly(gamap))
fig4